package com.itheima.com.itheima.controller;

import com.itheima.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "queryByIdFallBack")
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id) {
        String url = "http://USER-SERVICE/user/" + id;

        User userList = restTemplate.getForObject(url, User.class);

        return userList;

    }
//    服务降级方法
    public User queryByIdFallBack(Long id){
        User user = new User();
        user.setUserName("服务一不小心溜走了，请稍后再试试");

        return user;

    }

}
